package com.dlts.freemarker.model;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.fasterxml.jackson.annotation.JsonInclude;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

/**
 * @Classname R
 * @Description 返回对象
 * @Author 谢百庆
 * @Date 2022/6/6
 * @Time 10:57
 */
@Data
public class R<T> {

    public static final String SUCCESS_CODE = "0";
    public static final String SUCCESS_MSG="接口调用成功";
    public static final String ERROR_CODE = "-1";
    public static final String ERROR_MSG = "接口调用失败";

    @ApiModelProperty("返回码")
    private String code;

    @ApiModelProperty("返回描述")
    private String msg;

    @JsonInclude(JsonInclude.Include.NON_NULL)
    @ApiModelProperty("返回数据")
    private T data;

    @ApiModelProperty("总数")
    private long count;

    public static R ok(){
        R r =new R(SUCCESS_CODE,SUCCESS_MSG);
        return r;
    }

    public static R ok(String msg){
        R r =new R(SUCCESS_CODE,msg);
        return r;
    }

    public static R ok(IPage page){
        R r = new R(SUCCESS_CODE, "查询成功");
        r.setCount(page.getTotal());
        r.setData(page.getRecords());
        return r;
    }

    public static R error(){
        R r =new R(ERROR_CODE,ERROR_MSG);
        return r;
    }

    public static R error(String msg){
        R r =new R(ERROR_CODE,msg);
        return r;
    }

    public R() {
        this.code=SUCCESS_CODE;
        this.msg=SUCCESS_MSG;
    }

    public R(String code){
        this();
        this.code =code;
    }

    public R(String code, String msg){
        this(code);
        this.msg= msg;
    }

    public R(String code, String msg, T data) {
        this(code,msg);
        this.data = data;
    }

    public R<T> setCode(String code) {
        this.code = code;
        return this;
    }

    public R<T> setMsg(String msg) {
        this.msg = msg;
        return this;
    }

    public R<T> setData(T data) {
        this.data = data;
        return this;
    }

    public R<T> setCount(long count){
        this.count = count;
        return this;
    }
}
